package com.servlet;

import com.dao.TypeDao;
import com.dao.UserDao;
import com.entity.Type;
import com.entity.User;
import com.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author admin
 */
@WebServlet({"/TypeServlet.filter","/TypeUpdateServlet.filter","/TypeSelectOneServlet.filter","/TypeSelectAllServlet.filter",
        "/TypeDeleteServlet.filter","/TypeAddServlet.filter"})
public class TypeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("TypeServlet");
        String method = request.getParameter("method");
        System.out.println(method);
        if(method.equals("typeSelectList")) {
            this.selectTypeList(request, response);
        }else if (method.equals("typeSelectOne")){
            this.selectTypeOne(request, response);
        }else if (method.equals("deleteType")){
            this.deleteType(request, response);
        }else if (method.equals("addType")){
            this.addType(request, response);
        }else if (method.equals("updateType")){
            this.updateType(request, response);
        }else {
            response.sendRedirect(request.getContextPath()+"/TypeSelectAllServlet.filter?method=typeSelectList");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    private void selectTypeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession session = MybatisUtil.getSession();
        TypeDao typeDao = session.getMapper(TypeDao.class);
        List<Type> typeList = typeDao.typeSelectAll();
        System.out.println("typeList+" + typeList.size());
        request.setAttribute("typeList", typeList);
        request.getRequestDispatcher("/WEB-INF/page/type.jsp").forward(request, response);
    }

    private void selectTypeOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String t_id = request.getParameter("t_id");
        SqlSession session = MybatisUtil.getSession();
        TypeDao typeDao = session.getMapper(TypeDao.class);
        Type type = typeDao.typeSelectOne(Integer.parseInt(t_id));
        System.out.println(type.toString());
        request.setAttribute("type", type);
        request.getRequestDispatcher("/WEB-INF/page/type.jsp").forward(request, response);
    }

    private void deleteType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String t_id = request.getParameter("t_id");
        SqlSession session = MybatisUtil.getSession();
        TypeDao typeDao = session.getMapper(TypeDao.class);
        boolean b = typeDao.deleteType(Integer.parseInt(t_id));
        session.commit();
        if (b) {
            System.out.println("类型删除成功");
            response.sendRedirect(request.getContextPath() + "/TypeSelectAllServlet.filter?method=typeSelectList");
        }
    }

    private void addType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String t_name = request.getParameter("t_name");
        System.out.println(t_name);
        SqlSession session = MybatisUtil.getSession();
        TypeDao typeDao = session.getMapper(TypeDao.class);
        boolean b = typeDao.insertType(t_name);
        session.commit();
        if (b){
            System.out.println("类型添加成功");
            response.sendRedirect(request.getContextPath()+"/TypeSelectAllServlet.filter?method=typeSelectList");
        }
    }

    private void updateType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Type type = new Type();
        String t_id = request.getParameter("t_id");
        String t_name = request.getParameter("t_name");
        System.out.println(t_id + t_name);
        type.setT_id(Integer.parseInt(t_id));
        type.setT_name(t_name);
        SqlSession session = MybatisUtil.getSession();
        TypeDao typeDao = session.getMapper(TypeDao.class);
        boolean b = typeDao.updateType(type);
        session.commit();
        if (b) {
            System.out.println("类型修改成功");
            response.sendRedirect(request.getContextPath() + "/TypeSelectAllServlet.filter?method=typeSelectList");
        }
    }
}
